#include "SDL.h"
#include "Container.h"
#include "Button.h"

#include "TextureLoader.h"
#include <crtdbg.h>
#include <iostream>
#include "GuiEvent.h"

#include "ScreenManager.h"
#include "ScreenTest.h"
#include "SDL_ttf.h"
#include "SDL_image.h"


int main(int argc, char** argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Quit();
 
	SDL_Window * window = SDL_CreateWindow("Tower Defense",100,50,800,588,SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
	SDL_Renderer  * rend = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
	
	SDL_ShowCursor(0);
	TTF_Init();
	IMG_Init(IMG_INIT_PNG);

	TextureLoader::getPtr()->setRenderer(rend);

	ScreenManager * mgr = ScreenManager::getPtr();
	mgr->setRenderer(rend);
	mgr->setWindow(window);
	mgr->setActiveScreen(new ScreenTest());

	bool end = false;
	SDL_Event e;

	double ms = 1000/35;
	
	
	while(!end)
	{
		while(SDL_PollEvent(&e))
		{
			if(e.type == SDL_QUIT)
			{
				end = true;
				break;
			}

			mgr->fireEventToActiveScreen(e);

	
		}
	
		double time = SDL_GetTicks();
		mgr->updateActiveScreen();
		mgr->drawActiveScreen();
		double c_time = SDL_GetTicks();
		if(time - c_time < ms)
		SDL_Delay(ms - (time - c_time));
	}

	ScreenManager::Destroy();

	SDL_DestroyRenderer(rend);
	SDL_DestroyWindow(window);

	TTF_Quit();
	_CrtDumpMemoryLeaks();
    return 0;
}